<?php
namespace jy_yxgoodserpfw;
defined('BY_JYA') or exit('error');
/*
	订单，V8打印机：58mm（32字符），V9：80mm（42字符）
	设置字体大小、居中及切纸、来单提示音：http://www.jinyunzn.com/manage/index.php?plugin=core&action=article.show&nosession=1&id=11（出处：进云手册）
*/
class printmsg extends \plugin{
	public function faprint($order_id=0,$merchid=0,$printerid=0){
		$isprint=false;
		$params=array('uniacid'=>$_SESSION['uniacid'],'print_status'=>1);
		$params['merchid']=$merchid;
		if($merchid){
			$table='jy_yxgoodserpfw_merchfalog';
		}else{
			$table='jy_yxgoodserpfw_falog';
		}
		$falog=pdo_get($table,array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$falog['merchname']=pdo_getcolumn('jy_merch_user',array('id'=>$falog['merchid'],'uniacid'=>$_SESSION['uniacid']),'merchname');
		$falog['kulog']=json_decode($falog['kulog'],true);
		$falog['desc']='内含'.count($falog['kulog']).'条出库记录';
		if($printerid){
			$printers=pdo_getall('jy_yxgoodserpfw_printer',array('id'=>$printerid,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$printers=pdo_getall('jy_yxgoodserpfw_printer',$params);
		}
		if(!$printers){
			exi('无可用打印机！！','error');
		}
		$time=date('Y-m-d',$falog['createtime']);
		foreach($printers as $p){
			$content='';
			$content=$p['print_top']."\n";
			$content .='生成时间:'.$time."\n";
			$goodsstr=$this->getgoodsstr($falog['kulog'],$p['type']);
			$content.=$goodsstr['goodsinfo'];
			if($merchid){
			}else{
				$content.='经销商：'.$falog['merchname']."\n";
				$content.='经销地址：'.$falog['address'];
			}
			$this->toprint($p,$content,$falog);
			pdo_update($table,array('isprint'=>1),array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
			$isprint=true;
		}
		return $isprint;
	}
	public function getgoodsstr($kulog,$ptype=2){
			if($ptype==1 || $ptype==8 || $ptype==9){
				$conttags['line']=$this->strlen_pad('-',30,'-')."\n";
			}elseif($ptype==2 || $ptype==3 || $ptype==7  || $ptype==10){
				$conttags['line']=$this->strlen_pad('-',47,'-')."\n";
			}
			$top=$this->strlen_pad('类别',6);
			$top .= '流水号';
			$goodsinfo=$conttags['line']."<B>".$top."\n</B>";
			$strnew = '';
			foreach($kulog as $g){
				$typetitle=$g['code_type']?'箱标':'盒标';
				$strnew .= $this->strlen_pad($typetitle,6);
				$strnew .= $g['sn']. "\n";
			}
			$goodsinfo .= $strnew;
			$goodsinfo2 .= "<S2>".$strnew."</S2>";
			$goodsinfo .= $conttags['line'];
			$goodsinfo2 .= $conttags['line'];
			return array('goodsinfo2'=>$goodsinfo2,'goodsinfo'=>$goodsinfo);
	}
	public function toprint($p,$content='',$order=array(),$daike=0,$print_type=0){
			//print_r($content);exit;
			if($p['type']==7 || $p['type']==9){
				//飞蛾打印机
				$content=str_replace('\n','<BR>',$content);
				$content=str_replace('<AM><B><S2>','<CB>',$content);
				$content=str_replace('</S2></B></AM>','</CB>',$content);
				$content=str_replace('<AM>','<C>',$content);
				$content=str_replace('</AM>','</C>',$content);
				$content=str_replace('<AR>','<RIGHT>',$content);
				$content=str_replace('</AR>','</RIGHT>',$content);
				$content=str_replace('<B>','<BOLD>',$content);
				$content=str_replace('</B>','</BOLD>',$content);
				$content=str_replace('<S1>','<BOLD>',$content);
				$content=str_replace('</S1>','</BOLD>',$content);
				$content=str_replace('<S2>','<B>',$content);
				$content=str_replace('</S2>','</B>',$content);
				$content=str_replace('<S3>','<B>',$content);
				$content=str_replace('</S3>','</B>',$content);
				$content=str_replace('<S4>','<B>',$content);
				$content=str_replace('</S4>','</B>',$content);
				$content=str_replace('<S5>','<B>',$content);
				$content=str_replace('</S5>','</B>',$content);
				$content=str_replace('<S6>','<B>',$content);
				$content=str_replace('</S6>','</B>',$content);
				$content=str_replace('<S7>','<B>',$content);
				$content=str_replace('</S7>','</B>',$content);
				$content=str_replace('<S8>','<B>',$content);
				$content=str_replace('</S8>','</B>',$content);
			}elseif($p['type']==8 || $p['type']==10){
				//芯烨打印机
				$content=str_replace('\n','<BR>',$content);
				$content=str_replace('<AM>','<C>',$content);
				$content=str_replace('</AM>','</C>',$content);
				$content=str_replace('<AR>','<R>',$content);
				$content=str_replace('</AR>','</R>',$content);
				$content=str_replace('<B>','<BOLD>',$content);
				$content=str_replace('</B>','</BOLD>',$content);
				$content=str_replace('<S1>','<N>',$content);
				$content=str_replace('</S1>','</N>',$content);
				$content=str_replace('<S2>','<B>',$content);
				$content=str_replace('</S2>','</B>',$content);
				$content=str_replace('<S3>','<B>',$content);
				$content=str_replace('</S3>','</B>',$content);
				$content=str_replace('<S4>','<B2>',$content);
				$content=str_replace('</S4>','</B2>',$content);
				$content=str_replace('<S5>','<B2>',$content);
				$content=str_replace('</S5>','</B2>',$content);
				$content=str_replace('<S6>','<B2>',$content);
				$content=str_replace('</S6>','</B2>',$content);
				$content=str_replace('<S7>','<B2>',$content);
				$content=str_replace('</S7>','</B2>',$content);
				$content=str_replace('<S8>','<B2>',$content);
				$content=str_replace('</S8>','</B2>',$content);
			}
			file_put_contents(ROOT_D.'/1.txt',$content);
			$printparams=array('sendcontent'=>$content,'id'=>$p['printerid']);
			if($print_type){	
				if($p['type']==7 || $p['type']==9){
					$this->plugin('jy_feieyun')->printer($printparams,'jy_yxgoodserpfw',true,$p['print_nums'],$p['tpl_type']);
				}elseif($p['type']==8 || $p['type']==10){
					model('jy_xpyun/model')->sendprint($printparams['id'],$printparams['sendcontent'],true,$p['print_nums'],$p['tpl_type']);
				}else{
					$this->plugin('jy_print')->printer($printparams,'jy_yxgoodserpfw',true,$p['print_nums']);
				}
			}
			return true;
	}
	public function sendNotice($params){
		$tag = ((isset($params['tag']) ? $params['tag'] : ''));
		$touser = ((isset($params['openid']) ? $params['openid'] : ''));
		if (empty($touser)) {
			return;
		}
	}
	public function strlen_pad($str,$alllen,$zifu='')//补齐空格
	{
		//真实字符串长度（一个中文=2）
		$len = (strlen($str)+mb_strlen($str,'UTF-8'))/2;
		$buqi = '';
		for($i=0;$i<($alllen-$len);$i++){
			$buqi .= $zifu?:' ';
		}
		$new_str = $str.$buqi;
		return $new_str;
	}
}
